﻿using System;
using System.Linq.Expressions;
using System.Reflection;
using Budget.Core.DomainModel;

namespace Budget.Tests.Infrastructure.Extensions
{
    static class EntityExtension
    {
         public static TEntity SetProperty<TEntity, TProperty>(this TEntity entity, Expression<Func<TEntity, TProperty>> property, TProperty value)
             where TEntity : BaseEntity
         {
             var expression = (MemberExpression)property.Body;

             PropertyInfo propertyInfo = entity.GetType().GetProperty(expression.Member.Name, BindingFlags.Instance | BindingFlags.Public);

             propertyInfo.SetValue(entity, value, null);

             return entity;
         }
    }
}